import Vue from 'vue';
import VueRouter from 'vue-router';
import CakeList from '@/components/CakeList.vue';
import Cart from '@/components/Cart.vue';
import Orders from '@/components/Orders.vue';
import Profile from '@/components/Profile.vue';
import Login from '@/components/Login.vue';
import Favorites from '@/components/Favorites.vue'; // 新增

Vue.use(VueRouter);

const router = new VueRouter({
  mode: 'history',
  routes: [
    { path: '/', component: CakeList }, 
    { path: '/login', component: Login },
    { path: '/cart', component: Cart, meta: { requiresAuth: true } },
    { path: '/orders', component: Orders, meta: { requiresAuth: true } },
    { path: '/profile', component: Profile, meta: { requiresAuth: true } },
    { path: '/favorites', component: Favorites } // 新增
  ]
});

router.beforeEach((to, from, next) => {
  const requiresAuth = to.matched.some(record => record.meta.requiresAuth);
  if (requiresAuth && !localStorage.getItem('user')) {
    next('/login');
  } else {
    next();
  }
});

export default router;